/**
 * Copyright (c) 2017-2018, zengjintao (1913188966@qq.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fast.framework.web.handler;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fast.framework.aop.GuiceManager;
import com.fast.framework.aop.InteceptorManager;
import com.fast.framework.aop.Invocation;
import com.fast.framework.constant.Constant.ViewType;
import com.fast.framework.web.core.Action;
import com.fast.framework.web.core.ActionReport;
import com.fast.framework.web.core.Config;
import com.fast.framework.web.resolver.JspViewResolver;
import com.fast.framework.web.resolver.ViewResolver;

/**
 * @version 1.0
 */
public class ActionHandler implements Handler{

	private ViewResolver viewResolver;
	
	private ActionMapping actionMapping;
	
	private static final GuiceManager guiceManger = GuiceManager.getGuiceManger();
	
	private static final Config config = Config.getConfig();
	
	private static final InteceptorManager inteceptorManager = InteceptorManager.getInteceptorManager();
	
	public ActionMapping getActionMapping() {
		return actionMapping;
	}

	public void setActionMapping(ActionMapping actionMapping) {
		this.actionMapping = actionMapping;
	}

	static final ActionHandler actionHandler = new ActionHandler();
	
	private ActionHandler(){
		
	}

	public static ActionHandler me(){
		return actionHandler;
	}
	
	@Override
	public void doHandler(String target,HttpServletRequest request,HttpServletResponse response){
		String[] urlPara = {null};
		Action action = config.getAction(target,urlPara);
		if(action == null)
			throw new RuntimeException("can not found the action "+target);
		try {
			Object controller = action.getControllerClass().newInstance();
			guiceManger.getInjector().injectMembers(controller);//guice依赖注入
			Invocation invocation =	new Invocation(action, controller,inteceptorManager.getInterceptorsToArray(),urlPara);
    		invocation.invoke(request,response);
    		ActionReport.report(target, action,request);
			Object object = invocation.getReturnValue();
			Class<?> returnType = action.getMethod().getReturnType();
			if(returnType == String.class){
				String returnValue = (String) object;
				if(Config.getJfastconfig().getViewType().equalsIgnoreCase(ViewType.JSP.toString())){
					viewResolver = new JspViewResolver(returnValue,request,response);
				}else if (Config.getJfastconfig().getViewType().equalsIgnoreCase(ViewType.VELOCITY.toString())) {
					viewResolver = new JspViewResolver(returnValue,request,response);
				}else if (Config.getJfastconfig().getViewType().equalsIgnoreCase(ViewType.JFINAL_VIEW.toString())) {
					viewResolver = new JspViewResolver(returnValue,request,response);
				}
				viewResolver.render();
			}else{//json数据
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
